// =================================================================================================
//
//	CopyEngine Framework
//	Copyright 2012 Eran. All Rights Reserved.
//
//	This program is free software. You can redistribute and/or modify it
//	in accordance with the terms of the accompanying license agreement.
//
// =================================================================================================

package
{
    import copyengine.utils.debug.CELog;
    import copyengine.utils.debug.TraceLog;

    import flash.display.Loader;
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.net.URLRequest;
    import flash.system.ApplicationDomain;

    import gui.component.fileOperate.FileOperateLayer;
    import gui.component.mapEditor.MapEditorLayer;
    import gui.component.mapSetting.MapSettingLayer;
    import gui.manger.GUIManger;
    import gui.manger.GUIMangerDTO;
    import gui.utils.blockEditor.BlockEditorDialog;
    import gui.utils.ConfirmBox;
    import gui.utils.MessageBox;

    public class MapEditor extends Sprite
    {
        private var mGUILoader:Loader;
        private var mGUIDomain:ApplicationDomain;
        private var mGUIMc:MovieClip;

        private var mConfirmBoxLayer:Sprite;
        private var mBlockEditorLayer:Sprite;

        private var mGUIManger:GUIManger;
        private var mFileOperateLayer:FileOperateLayer;
        private var mMapEditorLayer:MapEditorLayer;
        private var mMapSettingLayer:MapSettingLayer;

        public function MapEditor()
        {
            super();
            CELog.setLogImpl(new TraceLog());

            mGUILoader = new Loader();
            mGUILoader.load(new URLRequest("GUI.swf"));
            mGUILoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadGUIComplete);
        }

        private function onLoadGUIComplete(_e:Event):void
        {
            mGUIDomain = mGUILoader.contentLoaderInfo.applicationDomain;

            var GUIClass:Class = mGUIDomain.getDefinition("GUI") as Class;
            mGUIMc = new GUIClass();

            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            stage.stageWidth = mGUIMc.width;
            stage.stageHeight = mGUIMc.height;
            this.addChild(mGUIMc);

            initialize();
        }

        //=================================//
        //=== Initialize & Dispose ========//
        //=================================//

        private function initialize():void
        {
            var dto:GUIMangerDTO = new GUIMangerDTO();
            mGUIManger = new GUIManger();

            mFileOperateLayer = new FileOperateLayer();
            mFileOperateLayer.initialize(mGUIMc["fileLayerMc"], mGUIManger);

            mMapEditorLayer = new MapEditorLayer();
            mMapEditorLayer.initialize(mGUIMc["mapEditLayerMc"], mGUIManger);

            mMapSettingLayer = new MapSettingLayer();
            mMapSettingLayer.initialize(mGUIMc["mapSettingLayerMc"], mGUIManger);

            dto.fileOperate = mFileOperateLayer;
            dto.mapEditor = mMapEditorLayer;
            dto.mapSetting = mMapSettingLayer;
            mGUIManger.initialize(dto);

            //==============
            //==Message Box
            //==============
            MessageBox.instance.initialize(mGUIDomain.getDefinition("Message") as Class, mGUIMc);

            //=====================
            //==BlockEditor Dialog
            //=====================
            mBlockEditorLayer = new Sprite();
            this.addChild(mBlockEditorLayer);
            BlockEditorDialog.instance.initialize(mBlockEditorLayer,
                                                  mGUIDomain.getDefinition("BlockEditorDialog") as Class);

            //=================
            //==Confirm Dialog
            //=================
            mConfirmBoxLayer = new Sprite();
            this.addChild(mConfirmBoxLayer);
            ConfirmBox.instance.initialize(mConfirmBoxLayer, mGUIDomain.getDefinition("ConfirmBox") as Class);

        }


    }
}
